package com.appkubes.mall.admin.application.order.goods.dao;

import com.appkubes.mall.admin.application.order.order.entity.model.GetLeaderListResult;
import com.appkubes.mall.admin.application.order.order.entity.model.GetOrderGoodListResult;
import com.appkubes.mall.core.dao.BaseMallOrderGoodsDao;
import com.appkubes.mall.core.entity.MallOrderGoods;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface OrderGoodsDao extends BaseMallOrderGoodsDao {

    @Delete({"<script>",
            "Delete from mall_ordergoods where ordergoods_key in ",
            "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'> #{id} </foreach> ",
            "</script>"})
    int delete(@Param("ids") String[] ids);

    @Update({"<script>",
            "update mall_ordergoods set state='complete' where state='arrive' and ordergoods_key in ",
            "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'> #{id} </foreach> ",
            "</script>"})
    int onConfirmOrder(@Param("ids") String[] ids);

    @Select({"<script>",
            "SELECT " +
                    "<choose><when test='isTotal!=true'>" +
                    " leader_key,goods_key,goods_name,attr_keys,attr_names,state,sum(num)as num " +
                    "</when>" +
                    "<otherwise>" +
                    " count(leader_key) as total" +
                    "</otherwise></choose>" +
                    " FROM  `mall_ordergoods` WHERE `mall_ordergoods`.`app_key`=#{appKey} AND ( ( state != 'unPay' ) AND `app_key`=#{appKey} ) " +
                    " AND (goods_name like '%${key}%' or attr_names like '%{$key}%') " +
                    "<when test='state!=-10'>" +
                    " AND state=${state}  " +
                    "</when>" +
                    "<when test='isTotal!=true'>" +
                    " GROUP BY `leader_key`,`goods_key`,`goods_name`,`attr_keys`,`attr_names`,`state` LIMIT #{page},#{limit} " +
                    "</when>",
            "</script>"})
    List<GetLeaderListResult> getList(@Param("appKey") String appKey,
                                      @Param("key") String key,
                                      @Param("state") int state,
                                      @Param("page") int page,
                                      @Param("limit") int limit,
                                      @Param("isTotal") boolean isTotal);

    //UserAccountHistoryDao引用
    @Select("SELECT * FROM `mall_order_goods` where `mall_order_goods`.`order_key`=#{orderKey}")
    List<MallOrderGoods> getListByOrderKeyResult(@Param("orderKey") String orderKey);

    @Select({"<script>",
            "SELECT " +
                    "<choose><when test='isTotal!=true'>" +
                    " `og`.*,o.order_no," +
                    " o.create_time as order_time, " +
                    " u.name as user_name,u.tel as user_tel, " +
                    " l.name as leader_name,l.tel as leader_tel,l.address as leader_address,l.community as leader_community, " +
                    " g.supplier_name as goods_supplier_name,g.supplier_tel as goods_supplier_tel " +
                    "</when>" +
                    "<otherwise>" +
                    " count(`og`.id) as total" +
                    "</otherwise></choose>" +
                    " FROM `mall_ordergoods` `og`" +
                    " INNER JOIN `mall_user` `u` ON `og`.`user_key`=`u`.`user_key` " +
                    " INNER JOIN `mall_order` `o` ON `og`.`order_key`=`o`.`order_key` " +
                    " INNER JOIN `mall_leader` `l` ON `og`.`leader_key`=`l`.`leader_key` " +
                    " INNER JOIN `mall_goods` `g` ON `og`.`goods_key`=`g`.`goods_key` " +
                    "<when test='queryStr!=null'>" +
                    " ${queryStr} " +
                    "</when>" +
                    " ORDER BY og.ordergoods_key DESC " +
                    "<when test='isTotal!=true'>" +
                    "LIMIT #{page},#{limit}" +
                    "</when>",
            "</script>"})
    List<GetOrderGoodListResult> getListResult(@Param("queryStr") String queryStr,
                                               @Param("page") int page,
                                               @Param("limit") int limit,
                                               @Param("isTotal") boolean isTotal
    );

    @Update({"<script>",
            "update mall_ordergoods set app_key=#{appKey} where order_key=#{orderKey} ",
            "</script>"})
    int updatePlanUuid(@Param("orderKey") String orderKey, @Param("appKey") String appKey);
}
